package com.wxprogram.smallprogram.service.impl;

import com.wxprogram.smallprogram.dao.IFindstudent;
import com.wxprogram.smallprogram.dao.IFindteacher;
import com.wxprogram.smallprogram.dao.SaveDao;
import com.wxprogram.smallprogram.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wxprogram.smallprogram.dao.UserInfoDao;
import com.wxprogram.smallprogram.entity.UserStudent;
import com.wxprogram.smallprogram.entity.UserTeacher;
import com.wxprogram.smallprogram.service.UserInfoReqService;

import java.util.List;

@Service
public class UserInfoReqServiceImpl implements UserInfoReqService {
	
	@Autowired
	private UserInfoDao userInfoDao;
	@Autowired
	private IFindstudent iFindstudent;
	@Autowired
	private IFindteacher iFindteacher;
	@Autowired
	private SaveDao saveDao;
	/**
	 * 查询及修改信息（学生，学长）
	 */
	@Override
	public UserTeacher getTeacherInfo(String openID) {
		
		return userInfoDao.getTeacherInfo(openID);
	}

	@Override
	public UserStudent getStudentInfo(String openID) {
		
		return userInfoDao.getStudentInfo(openID);
	}

	/**
	 * 修改研究生身份为学生
	 * @param userTeacher
	 * @return
	 */
	@Override
	public int updateTeacherInfo(UserTeacher userTeacher) {
//		List<Integer> list= iFindstudent.selectTeaCodeByOpenId(userTeacher.getTeacheropenID());
		List<Integer> list = saveDao.selectTeacherSaveCode(userTeacher.getTeacheropenID());
		int count = userInfoDao.deleteTeacherInfo(userTeacher.getTeacheropenID());
		if (count > 0){
			if (list != null && !list.isEmpty()){
				for (int code:list){
					iFindteacher.updateSaveNum(-1,code);
				}
			}
//			if (list != null && !list.isEmpty()){
//				for (int code:list) {
//					final int _code = code;
//					new Thread(new Runnable(){
//						@Autowired
//						private FileService fileService;
//
//						@Override
//						public void run() {
//							String path = iFindstudent.selectImgByCode(_code);
//							fileService.deleteFile(path);
//						}
//					}).start();
//				}
//			}

            UserStudent userStudent = new UserStudent();
            userStudent.setStuopenID(userTeacher.getTeacheropenID());
            userStudent.setStudegree(userTeacher.getTeacherdegree());
		    return userInfoDao.insertStudent(userStudent);
		}else {
			return  0;
		}
	}

	/**
	 * 修改学生身份为研究生
	 * @param userStudent
	 * @return
	 */
	@Override
	public int updateStudentInfo(UserStudent userStudent) {
//		List<Integer> list = iFindteacher.selectStuCodeByOpenid(userStudent.getStuopenID());
		List<Integer> list = saveDao.selectStudentSaveCode(userStudent.getStuopenID());
		int count = userInfoDao.deleteStudentInfo(userStudent.getStuopenID());
		if(count > 0){
			if (list != null && !list.isEmpty()){
				for (int code:list){
					iFindstudent.updateSaveNum(-1,code);
				}
			}
//			if (list != null && !list.isEmpty()){
//				for (int code:list){
//					final int _code = code;
//					new Thread(new Runnable(){
//						@Autowired
//						private FileService fileService;
//
//						@Override
//						public void run() {
//							String path = iFindteacher.selectImgByCode(_code);
//							fileService.deleteFile(path);
//						}
//					}).start();
//				}
//			}
            UserTeacher userTeacher = new UserTeacher();
            userTeacher.setTeacheropenID(userStudent.getStuopenID());
            userTeacher.setTeacherdegree(userStudent.getStudegree());
            return userInfoDao.insertTeacher(userTeacher);
		}else{
			return 0;
		}
	}


	//检查是否存在用户，存在返回学位
	@Override
	public  int checkDegree(String openID){
		UserStudent userStudent = null;
		UserTeacher userTeacher = null;
		userStudent = userInfoDao.getStudentInfo(openID);
		if(userStudent!= null){
			return  userStudent.getStudegree();
		}else if((userTeacher = userInfoDao.getTeacherInfo(openID)) != null){
			return userTeacher.getTeacherdegree();
		}else {
			return  0;
		}

	}

	//添加学长
	@Override
	public int insertTeacher(UserTeacher userTeacher){
		return userInfoDao.insertTeacher(userTeacher);
	}
	//添加学生
	@Override
	public int insertStudent(UserStudent userStudent){
		return userInfoDao.insertStudent(userStudent);
	}

}
